/**
 * 获得搜索参数
 * @param params
 * @returns {Object}
 */
function getSearchParams(params) {
    var searchParams = new Object();
    if (params != undefined) {
        searchParams = params;
    }
    $('.SearchForm').each(function() {
        var param = $(this).val().trim();
        var val = $("input:radio[name='sex']:checked").val();
        if (val == 0) {
            searchParams['sex'] = 0;
        } else if (val == 1) {
            searchParams['sex'] = 1;
        }
        if (param == undefined)
            param = '';
        searchParams[$(this).attr('id')] = param;
    });
    return searchParams;
}
//清空搜索条件
$("#Reset").click(function() {
    $('.SearchForm').val('');
    $("input:radio[name='sex']").removeAttr('checked');
});
/**
 * 页面自加载
 */
$(function() {
    // $('#student_createtime').datepicker({
    //     format: "yyyy-mm-dd",
    //     todayBtn: "linked",
    //     autoclose: true,
    //     todayHighlight: true,
    //     clearBtn: true
    // });

    var cellwidth = ($(".box-content.table-responsive").width() - 55) / 11;
    var psval = $('#datatable_teacherinfo').attr('data-size');
    if (psval == undefined || psval == "") {
        psval = 10;
    }
    /**
     * easyui 生成表格数据
     */
    $('#datatable_teacherinfo').datagrid({
        singleSelect: true, //允许选择多行
        striped: true,
        idField: 'id',
        remoteSort: false,
        collapsible: true,
        fit: false,
        url: 'admin/Subject/getSubjectData', //数据源路径
        loadMsg: '请等待数据载入....',
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        pageSize: psval,
        pageList: [psval, psval * 2, psval * 3, psval * 4, psval * 5],
        columns: [[{
            field: 's_id',
            title: '课程ID',
            align: 'center',
            width: cellwidth,
            sortable: true
        }, {
            field: 's_name',
            title: '课程名称',
            align: 'center',
            width: cellwidth,
            sortable: true,
        }]],
        onBeforeLoad: function (param) {
            param = getSearchParams(param);
        },
    });


    $('#Search').click(function () {
        $('#datatable_teacherinfo').datagrid('reload');
    });

    //添加
    $('.addSubject').click(function () {
        var content = "";
        $.ajax({
            url: 'admin/Subject/addSubject',
            type: 'POST',
            dataType: 'HTML',//返回的数据类型
            success: function (updatemodalhtml) {
                content = updatemodalhtml;
                //弹出添加框
                $.TeachDialog({
                    title: '新增课程信息',
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    otherButtons: ['保存'],
                    otherButtonStyles: ['btn-primary'],
                    clickButton: function (sender, modal, index) {
                        var formData = new FormData($("#addForm" )[0]);
                        $.ajax({
                            url: 'admin/Subject/insert',
                            data: formData,
                            type: 'POST',
                            dataType: 'JSON',
                            async: false,//1，这四个要写上，否则点击没反应
                            cache: false,//2
                            contentType: false,//3
                            processData: false,//4
                            success: function (returnData) {
                                if (returnData.status == 0) {
                                    //
                                    layer.msg(returnData.msg,{icon:5,time:2000});
                                    // layer.open({
                                    //     content: returnData.msg
                                    // })
                                } else {
                                    modal.modal('hide');
                                    // $.TeachDialog({
                                    //     content: returnData.msg
                                    // });
                                    layer.msg(returnData.msg,{icon:6,time:2000});
                                    // layer.open({
                                    //     content: returnData.msg
                                    // })
                                    $('#datatable_teacherinfo').datagrid('reload');
                                }
                            },
                            error: function () {
                                $.TeachDialog({
                                    content: '系统异常，请联系管理员',
                                });
                            },

                        });

                    },
                });
            },
            error: function () {
                $.TeachDialog({
                    content: '系统异常，请联系管理员',
                });
                return;
            }
        });
    });


    //编辑
    $('.editSubject').click(function () {
        var row = $('#datatable_teacherinfo').datagrid('getSelections');
        if (row.length<1) {
            $.TeachDialog({
                content: '请选择一行数据进行修改！',
            });
            return;
        }
        var content = "";
        $.ajax({
            url: 'admin/Subject/updateSubject',
            data: "nid=" + row[0].s_id,
            type: 'POST',
            dataType: 'HTML',//返回的数据类型
            success: function (updatemodalhtml) {
                content = updatemodalhtml;
                //弹出修改框
                $.TeachDialog({
                    title: '修改课程信息',
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    otherButtons: ['保存'],
                    otherButtonStyles: ['btn-primary'],
                    clickButton: function (sender, modal, index) {
                        var formData = new FormData($("#addForm" )[0]);
                        $.ajax({
                            url: 'admin/Subject/update',
                            data: formData,
                            type: 'POST',
                            dataType: 'JSON',
                            async: false,//1，这四个要写上，否则点击没反应
                            cache: false,//2
                            contentType: false,//3
                            processData: false,//4
                            success: function (returnData) {
                                if (returnData.status == 0) {
                                    // $.TeachDialog({
                                    //     content: returnData.msg
                                    // });
                                    layer.msg(returnData.msg,{icon:5,time:2000});
                                } else {
                                    modal.modal('hide');
                                    // $.TeachDialog({
                                    //     content: returnData.msg
                                    // });
                                    layer.msg(returnData.msg,{icon:6,time:2000});
                                    $('#datatable_teacherinfo').datagrid('reload');
                                }
                            },
                            error: function () {
                                $.TeachDialog({
                                    content: '系统异常，请联系管理员'
                                });
                            },

                        });

                    },
                });
            },
            error: function () {
                $.TeachDialog({
                    content: '系统异常，请联系管理员'
                });
                return;
            }
        });
    })

    /**
     * 删除
     */
    $('.deleteSubject').click(function() {
        var row = $('#datatable_teacherinfo').datagrid('getSelections');
        if (row.length < 1) {
            $.TeachDialog({
                content: '请选择一行数据！',
            });
            return;
        }
        $.TeachDialog({
            content: "确认删除此课程？",
            showCloseButton: true,
            showCloseButtonName: '取消',
            otherButtons: ['确认'],
            otherButtonStyles: ['btn-danger'],
            clickButton: function(sender, modal, index) {
                modal.modal('hide');
                $.ajax({
                    url: 'admin/Subject/deleteSubject', //form action
                    dataType: 'JSON', //返回体类型
                    type: 'POST', // form type
                    data: "s_id=" + row[0].s_id,
                    success: function(returnData) {
                        if (returnData.status == 1) {
                            layer.msg(returnData.msg,{icon:6,time:2000});
                            $('#datatable_teacherinfo').datagrid('reload');
                        } else {
                            layer.msg(returnData.msg,{icon:5,time:2000});
                        }
                    },
                    error: function() {
                        $.TeachDialog({
                            content: '删除失败',
                        });
                    }
                });
            }
        })
    })

});
